package com.quercus.dao.impl;

import com.quercus.dao.CustomerDao;
import com.quercus.entity.Customer;
import com.quercus.util.XmlUtil;
import org.dom4j.Document;
import org.dom4j.Element;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;


/**
 * @author quercus
 */
public class CustomerDaoImpl implements CustomerDao {

    String fileName;

    public CustomerDaoImpl() {
    }

    public CustomerDaoImpl(String fileName) {
        this.fileName = fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    static Document doc;

    @Override
    public List<Customer> findAll() {
        doc =  XmlUtil.readFile(fileName);
        List<Customer> customerList = new ArrayList();
        //2.获取所有的Customer信息
        List<Element> customerElemList = doc.getRootElement().elements("customer");
        //3.将Element内容转换到Customer对象中
        for (Element customerElem : customerElemList) {
            String cno = customerElem.attribute("cno").getText();
            int level = Integer.parseInt(customerElem.attribute("level").getText());
            int status = Integer.parseInt(customerElem.attribute("status").getText());

            String name = customerElem.element("name").getText();
            String sex = customerElem.element("sex").getText();
            String address = customerElem.element("address").getText();
            String remark = customerElem.element("remark").getText();
            String userId = customerElem.element("userId").getText();
            //看看如何处理日期数据的
            java.sql.Date birthDate = java.sql.Date.valueOf(customerElem.element("birthDate").getText());
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String soperateTime = customerElem.element("operateTime").getText();
            Date operateTime = null;
            try {
                operateTime = sdf.parse(soperateTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //看看如何处理List数据的
            List<String> phoneList = new ArrayList<>();
            List<Element> phoneElemList = customerElem.element("phoneList").elements("phone");
            for (Element phoneElem : phoneElemList) {
                String phone = phoneElem.getText();
                phoneList.add(phone);
            }
            Customer customer = new Customer(cno,
                    name,
                    sex,
                    birthDate,
                    address,
                    phoneList,
                    remark,
                    userId,
                    level,
                    status,
                    operateTime);
            customerList.add(customer);
        }
        //4.返回客户列表
        return customerList;
    }

    @Override
    public Customer findById(String cno) {
        Customer customer = null;
        //获取所有的Customer信息
        List<Element> customerElemList = doc.getRootElement().elements("customer");
        //3.将Element内容转换到Customer对象中
        for (Element customerElem : customerElemList) {
            String cno2 = customerElem.attribute("cno").getText();
            if (cno.equals(cno2)) {
                int level = Integer.parseInt(customerElem.attribute("level").getText());
                int status = Integer.parseInt(customerElem.attribute("status").getText());
                String name = customerElem.element("name").getText();
                String sex = customerElem.element("sex").getText();
                String address = customerElem.element("address").getText();
                String remark = customerElem.element("remark").getText();
                String userId = customerElem.element("userId").getText();
                //看看如何处理日期数据的
                Date birthDate = java.sql.Date.valueOf(customerElem.element("birthDate").getText());
                DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                String soperateTime = customerElem.element("operateTime").getText();
                Date operateTime = null;
                try {
                    operateTime = (Date) sdf.parse(soperateTime);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                //看看如何处理List数据的
                List<String> phoneList = new ArrayList<>();
                List<Element> phoneElemList = customerElem.element("phoneList").elements("phone");
                for (Element phoneElem : phoneElemList) {
                    String phone = phoneElem.getText();
                    phoneList.add(phone);
                }
                customer = new Customer(cno, name, sex, birthDate, address, phoneList, remark, userId, level, status, operateTime);
                //找到了就break
                break;
            }
        }
        //4.返回客户列表
        return customer;
    }

    @Override
    public void save(Customer customer) {
        //2.获取根元素customers
        Element rootElem = doc.getRootElement();
        //3.添加子元素customer
        Element customerElem = rootElem.addElement("customer");
        customerElem.addAttribute("cno", customer.getCno());
        customerElem.addAttribute("status", customer.getStatus() + "");
        customerElem.addAttribute("level", customer.getLevel() + "");
        customerElem.addElement("name").setText(customer.getName());
        customerElem.addElement("sex").setText(customer.getSex());
        customerElem.addElement("birthDate").setText(customer.getBirthDate().toString());
        customerElem.addElement("address").setText(customer.getAddress());
        customerElem.addElement("remark").setText(customer.getRemark());
        customerElem.addElement("userId").setText("");
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String operateTime = sdf.format(new Date());
        customerElem.addElement("operateTime").setText(operateTime);
        Element phoneListElem = customerElem.addElement("phoneList");
        for (Object phone : customer.getPhoneList()) {
            phoneListElem.addElement("phone").setText(phone.toString());
        }
        //4.写入文件
        XmlUtil.writeFile(doc, fileName);
    }

    @Override
    public void update(Customer customer) {
        //2.获取根元素customers
        Element rootElem = doc.getRootElement();
        //3.获取子元素customer
        List<Element> customerElemList = rootElem.elements("customer");
        for (int i = 0; i < customerElemList.size(); i++) {
            Element customerElem = customerElemList.get(i);
            String cno = customerElem.attribute("cno").getValue();
            if (cno.equals(customer.getCno())) {
                customerElem.element("address").setText(customer.getAddress());
                customerElem.element("remark").setText(customer.getRemark());
                customerElem.attribute("level").setValue(customer.getLevel() + "");
            }
        }
        XmlUtil.writeFile(doc, fileName);
    }

    @Override
    public void delete(String cno) {
        //2.获取根元素customers
        Element rootElem = doc.getRootElement();
        //3.添加子元素customer
        Iterator<Element> it = rootElem.elementIterator("customer");
        while (it.hasNext()) {
            Element customerElem = it.next();
            if (cno.equals(customerElem.attribute("cno").getValue())) {
                it.remove();
                break;
            }
        }
        //4.写入文件
        XmlUtil.writeFile(doc, fileName);
    }

    @Override
    public void updateUserId(String cno, String userId) {
        //2.获取根元素customers
        Element rootElem = doc.getRootElement();
        //3.添加子元素customer
        List<Element> customerElemList = rootElem.elements("customer");
        for (int i = 0; i < customerElemList.size(); i++) {
            Element customerElem = customerElemList.get(i);
            String cnoFromData = customerElem.attribute("cno").getValue();
            if (cno.equals(cnoFromData)) {
                customerElem.element("userId").setText(userId);
            }
        }
        //4.写入文件
        XmlUtil.writeFile(doc, fileName);
    }
}
